﻿#include <QCoreApplication>
#include "Public.h"
#include "CSqlOper.h"
#include "CGlobal.h"
#include "CDevDataBeltRealign.h"

bool CSqlOper::initSql()
{
	bool bRet=m_pDbDllOper->initSql();
	m_pDbDllOper->startWork();
	Sleep(1000);
	return bRet;
}

bool CSqlOper::addDefaultPoint(QString sPointGuid, QString sPointName,QString sBeltName, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_realign_point_config";
	cData.sSql = "insert into pai_belt_realign_point_config (POINT_GUID,DEVICE_IP,DEVICE_PORT,CAMERA_IP,CAMERA_PORT,"
		"CAMERA_USERNAME,CAMERA_PASSWORD,POINT_NAME,IS_DELETE,IS_HAVE_PLC,REALIGN_SET_VALUE,REALIGN_SET_VALUE_TWO,ALARM_TIME,PIC_PATH,IS_HAVE_BELT_STATE,LEFT_RATIO,RIGHT_RATIO,BELT_NAME,RTSP_URL)"
		" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

	cData.addValue("POINT_GUID", sPointGuid);
	cData.addValue("DEVICE_IP", "127.0.0.1");
	cData.addValue("DEVICE_PORT", 502);
	cData.addValue("CAMERA_IP", "127.0.0.1");

	cData.addValue("CAMERA_PORT", 8000);
	cData.addValue("CAMERA_USERNAME", "admin");
	cData.addValue("CAMERA_PASSWORD", "ky002380");
	cData.addValue("POINT_NAME", sPointName);
	cData.addValue("IS_DELETE", 0);

	cData.addValue("IS_HAVE_PLC", 0);
	cData.addValue("REALIGN_SET_VALUE", 50);
	cData.addValue("REALIGN_SET_VALUE_TWO", 30);
	cData.addValue("ALARM_TIME", 60);
	cData.addValue("PIC_PATH", "D:/RobotResult/");

	cData.addValue("IS_HAVE_BELT_STATE", 0);
	cData.addValue("LEFT_RATIO", 1);
	cData.addValue("RIGHT_RATIO", 1);
	cData.addValue("BELT_NAME", sBeltName);
	cData.addValue("RTSP_URL", "");
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	return bRet;
}

bool CSqlOper::selectDevInfoName(const QString &sDevGuid, QVector<QSqlRecord>& vecRecord, QString &sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_realign_point_config";
	cData.sSql = "select * from pai_belt_realign_point_config where POINT_GUID=?";
	cData.addValue("POINT_GUID", sDevGuid);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	vecRecord = cData.vecRecord;
	return bRet;
}

bool CSqlOper::selectCorrectInfo(const QString &sPointGuid, CDevDataBeltRealign* pDevData, QString &sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_realign_point_config";
	cData.sSql = "select * from pai_belt_realign_point_config where POINT_GUID=?";
	cData.addValue("POINT_GUID", sPointGuid);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	QVector<QSqlRecord> vecDataInfo = cData.vecRecord;
	for (int i = 0; i < vecDataInfo.size(); i++)
	{
		pDevData->m_sPointGuid = vecDataInfo[i].value("POINT_GUID").toString();
		pDevData->m_sBeltName = vecDataInfo[i].value("BELT_NAME").toString();
		pDevData->m_sPointName = vecDataInfo[i].value("POINT_NAME").toString();
		pDevData->m_nIsHaveDev = vecDataInfo[i].value("IS_HAVE_PLC").toInt();
		pDevData->m_nIsHaveBeltRunState = vecDataInfo[i].value("IS_HAVE_BELT_STATE").toInt();
		pDevData->m_nBeltStateType = vecDataInfo[i].value("BELTSTATE_TYPE").toInt();
		pDevData->m_nRealignSetValue = vecDataInfo[i].value("REALIGN_SET_VALUE").toInt();
		pDevData->m_nRealignSetValueTwo = vecDataInfo[i].value("REALIGN_SET_VALUE_TWO").toInt();
		pDevData->m_nAlarmTime = vecDataInfo[i].value("ALARM_TIME").toInt();
		pDevData->m_sRecordPath = vecDataInfo[i].value("PIC_PATH").toString();
		pDevData->m_sPlcIpAddress = vecDataInfo[i].value("DEVICE_IP").toString();
		pDevData->m_nPlcPort = vecDataInfo[i].value("DEVICE_PORT").toInt();
		pDevData->m_nResultNum = vecDataInfo[i].value("RESULT_TIME").toInt();
		pDevData->m_pDevDataCam->m_sDevGuid = sPointGuid;
		pDevData->m_pDevDataCam->m_sCamerName = pDevData->m_sPointName;
		pDevData->m_pDevDataCam->m_sCamerIp = vecDataInfo[i].value("CAMERA_IP").toString();
		pDevData->m_pDevDataCam->m_nCamerPort = vecDataInfo[i].value("CAMERA_PORT").toInt();
		pDevData->m_pDevDataCam->m_sUserName = vecDataInfo[i].value("CAMERA_USERNAME").toString();
		pDevData->m_pDevDataCam->m_sPassWord = vecDataInfo[i].value("CAMERA_PASSWORD").toString();
		pDevData->m_pDevDataCam->m_sRtspUrlImg = vecDataInfo[i].value("RTSP_URL").toString();
	}
	return bRet;
}

bool CSqlOper::selectCorrectResult(QString sPointGuid, QVector<QSqlRecord>& vecRecord, QString sStartTime, QString sEndTime, int nAlarm, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_correct_result";
	cData.sSql = QString("select * from pai_belt_correct_result where RECTIFY_START_TIME >= str_to_date('%1','%Y/%m/%d %T') and RECTIFY_START_TIME <= str_to_date('%2','%Y/%m/%d %T') and POINT_GUID='%3' and ALARM_TYPE=%4 order by RECTIFY_START_TIME desc")
		.arg(sStartTime)
		.arg(sEndTime)
		.arg(sPointGuid)
		.arg(nAlarm);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	vecRecord = cData.vecRecord;
	return bRet;
}

bool CSqlOper::selectCorrectData(QString sDevGuid, QVector<QSqlRecord>& vecRecord, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_realign_point_config";
	cData.sSql = "select * from pai_belt_realign_point_config where IS_DELETE=? and DEVICE_GUID=?";
	cData.addValue("IS_DELETE", 0);
	cData.addValue("DEVICE_GUID", sDevGuid);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	vecRecord = cData.vecRecord;
	return bRet;
}

bool CSqlOper::selectIoAllocationByID(CDevDataBeltRealign* pDevData, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_io_allocation_table";
	cData.sSql = "select * from pai_io_allocation_table where POINT_GUID=?";
	cData.vecField.clear();
	cData.vecValue.clear();
	cData.vecField.push_back("POINT_GUID");
	cData.vecValue.push_back(pDevData->m_sPointGuid);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	QVector<QSqlRecord> vecDataInfo = cData.vecRecord;
	QMap<int, SAddrData> m_mapAddrDataR;        //读 测点序号 测点地址结构体
	QMap<int, SAddrData> m_mapAddrDataW;        //写 测点序号 测点地址结构体
	for (int i = 0; i < vecDataInfo.size(); i++)
	{
		SAddrData stAddrData;
		stAddrData.m_sAddrData = vecDataInfo[i].value("START_ADDRESS").toString();
		stAddrData.m_nAddrLength = vecDataInfo[i].value("DATA_LENGTH").toInt();
		stAddrData.m_nOffset = vecDataInfo[i].value("OFFSET").toInt();
		if (0 == vecDataInfo[i].value("TYPE").toInt())//测点类型为读测点
		{
			pDevData->m_mapAddrDataR.insert(vecDataInfo[i].value("IO_TYPE").toInt(), stAddrData);
		}
		else if (1 == vecDataInfo[i].value("TYPE").toInt())//测点类型为写测点
		{
			pDevData->m_mapAddrDataW.insert(vecDataInfo[i].value("IO_TYPE").toInt(), stAddrData);
		}
	}
	return bRet;
}

bool CSqlOper::addCorrectPoint(QString sPointGuid, QString sPointName, QString PlcIP, int nPlcPort, QString sCamerIP, int nCamerPort, QString sCamerUser, QString sPassWd, 
	int nIsHavePlc, int nSetValue, int nSetValueTwo, int nAlarmTime, QString sPicPath, int nIsHaveBeltState, double dLeftRaito, double dRightRatio, QString sBeltName,QString sRtsp, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_realign_point_config";
	cData.sSql = "insert into pai_belt_realign_point_config (POINT_GUID,DEVICE_IP,DEVICE_PORT,CAMERA_IP,CAMERA_PORT,"
		"CAMERA_USERNAME,CAMERA_PASSWORD,POINT_NAME,IS_DELETE,IS_HAVE_PLC,REALIGN_SET_VALUE,REALIGN_SET_VALUE_TWO,ALARM_TIME,PIC_PATH,IS_HAVE_BELT_STATE,LEFT_RATIO,RIGHT_RATIO,BELT_NAME,RTSP_URL)"
		" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

	cData.addValue("POINT_GUID", sPointGuid);
	cData.addValue("DEVICE_IP", PlcIP);
	cData.addValue("DEVICE_PORT", nPlcPort);
	cData.addValue("CAMERA_IP", sCamerIP);

	cData.addValue("CAMERA_PORT", nCamerPort);
	cData.addValue("CAMERA_USERNAME", sCamerUser);
	cData.addValue("CAMERA_PASSWORD", sPassWd);
	cData.addValue("POINT_NAME", sPointName);
	cData.addValue("IS_DELETE", 0);

	cData.addValue("IS_HAVE_PLC", nIsHavePlc);
	cData.addValue("REALIGN_SET_VALUE", nSetValue);
	cData.addValue("REALIGN_SET_VALUE_TWO", nSetValueTwo);
	cData.addValue("ALARM_TIME", nAlarmTime);
	cData.addValue("PIC_PATH", sPicPath);

	cData.addValue("IS_HAVE_BELT_STATE", nIsHaveBeltState);
	cData.addValue("LEFT_RATIO", dLeftRaito);
	cData.addValue("RIGHT_RATIO", dRightRatio);
	cData.addValue("BELT_NAME", sBeltName);
	cData.addValue("RTSP_URL", sRtsp);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	return bRet;
}

bool CSqlOper::updateCorrectPoint(QString sPointGuid, QString sPointName, QString PlcIP, int nPlcPort, QString sCamerIP, int nCamerPort, QString sCamerUser, QString sPassWd, int nIsHavePlc,
	int nSetValue, int nSetValueTwo, int nAlarmTime, QString sPicPath, int nIsHaveBeltState, double dLeftRaito, double dRightRatio, QString sBeltName,QString sRtsp, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_realign_point_config";
	cData.sSql = "update pai_belt_realign_point_config set DEVICE_IP=?,DEVICE_PORT=?,CAMERA_IP=?,CAMERA_PORT=?,CAMERA_USERNAME=?,CAMERA_PASSWORD=?,POINT_NAME=?,IS_HAVE_PLC=?,REALIGN_SET_VALUE=?,REALIGN_SET_VALUE_TWO=?,ALARM_TIME=?,PIC_PATH=?,IS_HAVE_BELT_STATE=?,LEFT_RATIO=?,RIGHT_RATIO=?,BELT_NAME=?,RTSP_URL=? where POINT_GUID=?";

	cData.addValue("DEVICE_IP", PlcIP);
	cData.addValue("DEVICE_PORT", nPlcPort);
	cData.addValue("CAMERA_IP", sCamerIP);

	cData.addValue("CAMERA_PORT", nCamerPort);
	cData.addValue("CAMERA_USERNAME", sCamerUser);
	cData.addValue("CAMERA_PASSWORD", sPassWd);
	cData.addValue("POINT_NAME", sPointName);
	cData.addValue("IS_HAVE_PLC", nIsHavePlc);

	cData.addValue("REALIGN_SET_VALUE", nSetValue);
	cData.addValue("REALIGN_SET_VALUE_TWO", nSetValueTwo);
	cData.addValue("ALARM_TIME", nAlarmTime);
	cData.addValue("PIC_PATH", sPicPath);
	cData.addValue("IS_HAVE_BELT_STATE", nIsHaveBeltState);

	cData.addValue("LEFT_RATIO", dLeftRaito);
	cData.addValue("RIGHT_RATIO", dRightRatio);
	cData.addValue("BELT_NAME", sBeltName);
	cData.addValue("RTSP_URL", sRtsp);
	cData.addValue("POINT_GUID", sPointGuid);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	return bRet;
}

bool CSqlOper::insertCorrectPoint(QString sPointGuid, QString sPointName, QString PlcIP, int nPlcPort, QString sCamerIP, int nCamerPort, QString sCamerUser, QString sPassWd,
	int nIsHavePlc, int nSetValue, int nSetValueTwo, int nAlarmTime, QString sPicPath, int nIsHaveBeltState, double dLeftRaito, double dRightRatio, QString sBeltName, QString sRtsp, 
	QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_realign_point_config";
	cData.sSql = "insert into pai_belt_realign_point_config (POINT_GUID,DEVICE_IP,DEVICE_PORT,CAMERA_IP,CAMERA_PORT,"
		"CAMERA_USERNAME,CAMERA_PASSWORD,POINT_NAME,IS_DELETE,IS_HAVE_PLC,REALIGN_SET_VALUE,REALIGN_SET_VALUE_TWO,ALARM_TIME,PIC_PATH,IS_HAVE_BELT_STATE,LEFT_RATIO,RIGHT_RATIO,BELT_NAME,RTSP_URL)"
		" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

	cData.addValue("POINT_GUID", sPointGuid);
	cData.addValue("DEVICE_IP", PlcIP);
	cData.addValue("DEVICE_PORT", nPlcPort);
	cData.addValue("CAMERA_IP", sCamerIP);

	cData.addValue("CAMERA_PORT", nCamerPort);
	cData.addValue("CAMERA_USERNAME", sCamerUser);
	cData.addValue("CAMERA_PASSWORD", sPassWd);
	cData.addValue("POINT_NAME", sPointName);
	cData.addValue("IS_DELETE", 0);

	cData.addValue("IS_HAVE_PLC", nIsHavePlc);
	cData.addValue("REALIGN_SET_VALUE", nSetValue);
	cData.addValue("REALIGN_SET_VALUE_TWO", nSetValueTwo);
	cData.addValue("ALARM_TIME", nAlarmTime);
	cData.addValue("PIC_PATH", sPicPath);

	cData.addValue("IS_HAVE_BELT_STATE", nIsHaveBeltState);
	cData.addValue("LEFT_RATIO", dLeftRaito);
	cData.addValue("RIGHT_RATIO", dRightRatio);
	cData.addValue("BELT_NAME", sBeltName);
	cData.addValue("RTSP_URL", sRtsp);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	return bRet;
}

bool CSqlOper::deleteCorrectPoint(QString sPointGuid, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_realign_point_config";
	cData.sSql = "delete from pai_belt_realign_point_config where POINT_GUID=?";
	cData.addValue("POINT_GUID", sPointGuid);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	return bRet;
}

bool CSqlOper::selectAllocationTable(QString sPointGuid, QVector<QSqlRecord>& vecRecord, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_io_allocation_table";
	cData.sSql = "select * from pai_io_allocation_table where POINT_GUID=?";
	cData.addValue("POINT_GUID", sPointGuid);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	vecRecord = cData.vecRecord;
	return bRet;
}

bool CSqlOper::addRobModbus(QString sRobGuid, QList<QStringList> listModbusData, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_io_allocation_table";
	cData.sSql = "insert into pai_io_allocation_table (IO_TYPE,POINT_GUID,START_ADDRESS,DATA_LENGTH,ADDRESS_DESC,TYPE)"
		" values ";
	for (QStringList listData : listModbusData)
	{
		cData.sSql.append("(");
		cData.sSql.push_back(listData[0] + ",");
		cData.sSql.push_back("'" + sRobGuid + "',");
		cData.sSql.push_back("'" + listData[1] + "',");
		cData.sSql.push_back("'" + listData[2] + "',");
		cData.sSql.push_back("'" + listData[3] + "',");
		cData.sSql.push_back(listData[4]);
		cData.sSql.append("),");
	}
	cData.sSql.chop(1);

	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	return bRet;
}

bool CSqlOper::deleteAllocationTable(QString sPointGuid, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_io_allocation_table";
	cData.sSql = "delete from pai_io_allocation_table where POINT_GUID=?";
	cData.addValue("POINT_GUID", sPointGuid);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	return bRet;
}

bool CSqlOper::selectBeltWork(QVector<QSqlRecord>& vecDataInfo, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "ppub_belt_stp_info";
	cData.sSql = "select * from ppub_belt_stp_info";
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	vecDataInfo = cData.vecRecord;
	return bRet;
}

bool CSqlOper::selectBeltCorrect(QString sPointGuid, QVector<QSqlRecord>& vecRecord, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_realign_point_config";
	cData.sSql = "select * from pai_belt_realign_point_config where IS_DELETE=? and POINT_GUID=?";
	cData.addValue("IS_DELETE", 0);
	cData.addValue("POINT_GUID", sPointGuid);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	vecRecord = cData.vecRecord;
	return bRet;
}

bool CSqlOper::selectTagConfig(QString& sPointGuid, CDevDataBeltRealign* pDevData,QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_correct_tag_config";
	cData.sSql = "select * from pai_belt_correct_tag_config where POINT_GUID=?";
	cData.addValue("POINT_GUID", sPointGuid);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	QVector<QSqlRecord> vecDataInfo = cData.vecRecord;
	for (int i = 0; i < vecDataInfo.size(); i++)
	{
		QString sTagID= vecDataInfo[i].value("TAG_ID").toString();
		if (pDevData->m_mapInputPointAddr.contains(sTagID))
		{
			pDevData->m_mapInputPointAddr[sTagID]= vecDataInfo[i].value("TAG_ADDR").toString();
		}
	}
	return bRet;
}

bool CSqlOper::selectTagID(QString& sPointGuid, QString& sTagID, QVector<QSqlRecord>& vecRecord,QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_correct_tag_config";
	cData.sSql = "select * from pai_belt_correct_tag_config where POINT_GUID=? and TAG_ID=?";
	cData.addValue("POINT_GUID", sPointGuid);
	cData.addValue("TAG_ID", sTagID);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	vecRecord = cData.vecRecord;
	return bRet;
}

bool CSqlOper::insertTagID(QString& sPointGuid, QString& sTagID, QString& sTagAddr, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_correct_tag_config";
	cData.sSql = "insert into pai_belt_correct_tag_config (POINT_GUID,TAG_ID,TAG_ADDR)"
		" values(?,?,?)";

	cData.addValue("POINT_GUID", sPointGuid);
	cData.addValue("TAG_ID", sTagID);
	cData.addValue("TAG_ADDR", sTagAddr);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	return bRet;
}

bool CSqlOper::updateTagID(QString& sPointGuid, QString& sTagID, QString& sTagAddr, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_correct_tag_config";
	cData.sSql = "update pai_belt_correct_tag_config set TAG_ADDR = ? where POINT_GUID = ? and TAG_ID = ?";

	cData.addValue("TAG_ADDR", sTagAddr);
	cData.addValue("POINT_GUID", sPointGuid);
	cData.addValue("TAG_ID", sTagID);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	return bRet;
}

bool CSqlOper::selectDevInfo(QString& sDevGuid, QVector<QSqlRecord>& vecRecord, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_correct_devinfo";
	cData.sSql = "select * from pai_belt_correct_devinfo where DEV_GUID=?";
	cData.addValue("DEV_GUID", sDevGuid);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	vecRecord = cData.vecRecord;
	return bRet;
}

bool CSqlOper::insertDevInfo(QString& sDevGuid, QString& sDevName, QString& sPointGuid, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_correct_devinfo";
	cData.sSql = "insert into pai_belt_correct_devinfo (DEV_GUID,DEV_NAME,POINT_GUID)"
		" values(?,?,?)";

	cData.addValue("DEV_GUID", sDevGuid);
	cData.addValue("DEV_NAME", sDevName);
	cData.addValue("POINT_GUID", sPointGuid);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	return bRet;
}

bool CSqlOper::addCorrectResult(QString sReusltGuid, QString sPointGuid, QString sPointName, double dLeftValue, double dRightValue, QDateTime dStartTime, QString PicUrl, QString sHttpUrl, 
	QString AlarmDesc, QString sBeltName, int nAlarmType, int nRealignSetValue, int nRealignSetValueTwo, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_correct_result";
	cData.sSql = "insert into pai_belt_correct_result (GUID,POINT_GUID,POINT_NAME,LEFT_CURREENT_VALUE,RIGHT_CURREENT_VALUE,RECTIFY_START_TIME,"
		"PICTURE_URL,HTTP_PIC_URL,ALARM_DESC,BELT_NAME,INSERT_TIME,ALARM_TYPE,REALIGN_SET_VALUE,REALIGN_SET_VALUE_TWO)"
		" values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

	cData.addValue("GUID", sReusltGuid);
	cData.addValue("POINT_GUID", sPointGuid);
	cData.addValue("POINT_NAME", sPointName);
	cData.addValue("LEFT_CURREENT_VALUE", dLeftValue);
	cData.addValue("RIGHT_CURREENT_VALUE", dRightValue);

	cData.addValue("RECTIFY_START_TIME", dStartTime);
	cData.addValue("PICTURE_URL", PicUrl);
	cData.addValue("HTTP_PIC_URL", sHttpUrl);
	cData.addValue("ALARM_DESC", AlarmDesc);
	cData.addValue("BELT_NAME", sBeltName);

	cData.addValue("INSERT_TIME", QDateTime::currentDateTime());
	cData.addValue("ALARM_TYPE", nAlarmType);
	cData.addValue("REALIGN_SET_VALUE", nRealignSetValue);
	cData.addValue("REALIGN_SET_VALUE_TWO", nRealignSetValueTwo);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	return bRet;
}

bool CSqlOper::updateCorrectStopTime(QString sResultGuid, double dAvgLeftValue, double dAvgRight, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_correct_result";
	cData.sSql = "update pai_belt_correct_result set RECTIFY_END_TIME=?,LEFT_AVG_VALUE=?,RIGHT_AVG_VALUE=?,IS_CLOSE=? where GUID=?";
	cData.addValue("RECTIFY_END_TIME", QDateTime::currentDateTime());
	cData.addValue("LEFT_AVG_VALUE", dAvgLeftValue);
	cData.addValue("RIGHT_AVG_VALUE", dAvgRight);
	cData.addValue("IS_CLOSE", 1);
	cData.addValue("GUID", sResultGuid);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	return bRet;
}

bool CSqlOper::selectResultLastTime(QVector<QSqlRecord>& vecRecord, QString sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "result_record";
	cData.sSql = "SELECT COALESCE(MAX(RESULT_TIME),NOW()) AS latest_time FROM result_record where RESULT_TYPE = 3;";
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	vecRecord = cData.vecRecord;
	return bRet;
}

bool CSqlOper::selectRecRetInfo(QVector<QSqlRecord>& vecRecord, QString sStartTime, QString sEndTime, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_correct_result";
	cData.sSql = "SELECT * FROM pai_belt_correct_result WHERE INSERT_TIME > ? AND INSERT_TIME <?  AND IS_CLOSE = ? order by INSERT_TIME desc; ";
	cData.addValue("INSERT_TIME", sStartTime);
	cData.addValue("INSERT_TIME", sEndTime);
	cData.addValue("IS_CLOSE", 1);

	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	vecRecord = cData.vecRecord;
	return bRet;
}

bool CSqlOper::insertResult(QString sGuid, int nResultType, QString sResultName, QDateTime dTime, QString sPath, QString sHttpUrl, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "result_record";
	cData.sSql = "insert into result_record (GUID,RESULT_TYPE,RESULT_NAME,RESULT_TIME,RESULT_PATH,RESULT_PATH_URL)"
		" values(?,?,?,?,?,?)";

	cData.addValue("GUID", sGuid);
	cData.addValue("RESULT_TYPE", nResultType);
	cData.addValue("RESULT_NAME", sResultName);
	cData.addValue("RESULT_TIME", dTime);
	cData.addValue("RESULT_PATH", sPath);
	cData.addValue("RESULT_PATH_URL", sHttpUrl);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	return bRet;
}

bool CSqlOper::selectResult(QString sStartTime, QString sEndTime, QVector<QSqlRecord>& vecInfo, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "result_record";
	cData.sSql = QString("select * from result_record where RESULT_TIME >= str_to_date('%1','%Y/%m/%d %T') and RESULT_TIME <= str_to_date('%2','%Y/%m/%d %T') and RESULT_TYPE=3 ")
		.arg(sStartTime)
		.arg(sEndTime);
	cData.sSql += "order by RESULT_TIME desc";
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	vecInfo = cData.vecRecord;
	return bRet;
}

bool CSqlOper::deleteDevInfo(QString sDevId, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_correct_devinfo";
	cData.sSql = "delete from pai_belt_correct_devinfo where DEV_GUID =?";

	cData.addValue("DEV_GUID", sDevId);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	return bRet;
}

bool CSqlOper::deleteCamerInfo(QString sDevGuid, QString& sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "pai_belt_realign_point_config";
	cData.sSql = "delete from pai_belt_realign_point_config where DEV_GUID =?";

	cData.addValue("POINT_GUID", sDevGuid);
	bool bRet = m_pDbDllOper->exec(cData);
	sErrMsg = cData.sErr;
	return bRet;
}

CSqlOper::CSqlOper()
{
	//配置文件地址
	QString sFilePath = QCoreApplication::applicationDirPath() + A2T("/DLL/CorrectCfg/CorrectCfg.xml");
	QString sDllPath = QCoreApplication::applicationDirPath() + A2T("/SqlOper.dll");
	m_pDbDllOper = new CSqlDllOper(g_sTableName, sFilePath, sDllPath);
}

CSqlOper::~CSqlOper()
{
	delete m_pDbDllOper;
	m_pDbDllOper = nullptr;
}
